<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="formData" :rules="formRules" v-loading="formLoading" label-width="100px">
                    <el-form-item label="商品图片">
                      <ImageUpload v-model="formData.image"/>
                    </el-form-item>
                    <el-form-item label="商品介绍">
                      <Editor v-model="formData.description" :min-height="192"/>
                    </el-form-item>
                    <el-form-item label="商品成分" prop="ingredients">
                      <el-input v-model="formData.ingredients" placeholder="请输入商品成分" />
                    </el-form-item>
                    <el-form-item label="使用方法" prop="usageMethod">
                      <el-input v-model="formData.usageMethod" placeholder="请输入使用方法" />
                    </el-form-item>
                    <el-form-item label="商品规格" prop="specification">
                      <el-input v-model="formData.specification" placeholder="请输入商品规格" />
                    </el-form-item>
                    <el-form-item label="商品价格" prop="price">
                      <el-input v-model="formData.price" placeholder="请输入商品价格" />
                    </el-form-item>
                    <el-form-item label="点赞数" prop="likes">
                      <el-input v-model="formData.likes" placeholder="请输入点赞数" />
                    </el-form-item>
                    <el-form-item label="评论数" prop="comments">
                      <el-input v-model="formData.comments" placeholder="请输入评论数" />
                    </el-form-item>
                    <el-form-item label="收藏数" prop="favorites">
                      <el-input v-model="formData.favorites" placeholder="请输入收藏数" />
                    </el-form-item>
                    <el-form-item label="商品标题" prop="title">
                      <el-input v-model="formData.title" placeholder="请输入商品标题" />
                    </el-form-item>
                    <el-form-item label="商品分类名称" prop="productsClassifyName">
                      <el-input v-model="formData.productsClassifyName" placeholder="请输入商品分类名称" />
                    </el-form-item>
                    <el-form-item label="订单分类" prop="productsClassify">
                      <el-input v-model="formData.productsClassify" placeholder="请输入订单分类" />
                    </el-form-item>
      </el-form>
              <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import * as HotProductsApi from '@/api/app/hotproducts';
  import ImageUpload from '@/components/ImageUpload';
  import Editor from '@/components/Editor';
      export default {
    name: "HotProductsForm",
    components: {
          ImageUpload,
          Editor,
                    },
    data() {
      return {
        // 弹出层标题
        dialogTitle: "",
        // 是否显示弹出层
        dialogVisible: false,
        // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
        formLoading: false,
        // 表单参数
        formData: {
                            id: undefined,
                            image: undefined,
                            description: undefined,
                            ingredients: undefined,
                            usageMethod: undefined,
                            specification: undefined,
                            price: undefined,
                            likes: undefined,
                            comments: undefined,
                            favorites: undefined,
                            title: undefined,
                            productsClassifyName: undefined,
                            productsClassify: undefined,
        },
        // 表单校验
        formRules: {
                        image: [{ required: true, message: '商品图片不能为空', trigger: 'blur' }],
                        price: [{ required: true, message: '商品价格不能为空', trigger: 'blur' }],
                        likes: [{ required: true, message: '点赞数不能为空', trigger: 'blur' }],
                        comments: [{ required: true, message: '评论数不能为空', trigger: 'blur' }],
                        favorites: [{ required: true, message: '收藏数不能为空', trigger: 'blur' }],
        },
                        };
    },
    methods: {
      /** 打开弹窗 */
     async open(id) {
        this.dialogVisible = true;
        this.reset();
        // 修改时，设置数据
        if (id) {
          this.formLoading = true;
          try {
            const res = await HotProductsApi.getHotProducts(id);
            this.formData = res.data;
            this.title = "修改热门商品";
          } finally {
            this.formLoading = false;
          }
        }
        this.title = "新增热门商品";
              },
      /** 提交按钮 */
      async submitForm() {
        // 校验主表
        await this.$refs["formRef"].validate();
                  this.formLoading = true;
        try {
          const data = this.formData;
                  // 修改的提交
          if (data.id) {
            await HotProductsApi.updateHotProducts(data);
            this.$modal.msgSuccess("修改成功");
            this.dialogVisible = false;
            this.$emit('success');
            return;
          }
          // 添加的提交
          await HotProductsApi.createHotProducts(data);
          this.$modal.msgSuccess("新增成功");
          this.dialogVisible = false;
          this.$emit('success');
        } finally {
          this.formLoading = false;
        }
      },
                      /** 表单重置 */
      reset() {
        this.formData = {
                            id: undefined,
                            image: undefined,
                            description: undefined,
                            ingredients: undefined,
                            usageMethod: undefined,
                            specification: undefined,
                            price: undefined,
                            likes: undefined,
                            comments: undefined,
                            favorites: undefined,
                            title: undefined,
                            productsClassifyName: undefined,
                            productsClassify: undefined,
        };
        this.resetForm("formRef");
      }
    }
  };
</script>
